﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HustQQModel;
using HustQQInterface;

namespace HustQQClient.UI
{
    public partial class MainFrame : Form
    {
        public MainFrame()
        {
            InitializeComponent();
            labelNote.DisplayContent();
            InfoControl.dictSingleChat.Clear();
            contacts.BeginChat += new ContactDisplay.EnterEventHandler(contacts_BeginChat);
            contacts.BeginTeam += new ContactDisplay.TeamEventHandler(contacts_BeginTeam);
            InterfaceManager.NetManager.ConfirmRecieveFile += new FileMsg(NetManager_ConfirmRecieveFile);
            InterfaceManager.NetManager.RecieveTextFromSingle +=new SingleTextMsg(NetManager_RecieveTextFromSingle);       
            InterfaceManager.NetManager.RecieveTextFromTeam += new TeamTextMsg(NetManager_RecieveTextFromTeam);
        }
        void NetManager_RecieveTextFromSingle(string userId, string text, DateTime time)
        {   
            this.Invoke(new Action(() =>
                {
                    User curUsr = FindFriendById(userId);
                    if (curUsr == null) return;
                    SingleChat currentChat = contacts_BeginChat
                        (curUsr.name, curUsr.qq_ID, curUsr.note, InfoControl.FaceList[curUsr.face]);
                    currentChat.DisplayTextInfo(curUsr.name, text, time.ToString());
                }));
        }
        
        User FindFriendById(string id)
        {
            foreach (User u in InfoControl.CurrentUser.friends)
            {
                if (id == u.qq_ID)
                {
                    return u;
                }
            }
            return null;
        }
        void NetManager_RecieveTextFromTeam(string userId, string teamId, string text, DateTime time)
        {
            this.Invoke(new Action(() =>
            {
                contacts_BeginTeam(teamId).ReceiveTeamMsg(userId, teamId, text, time);
            }));
        }

        void NetManager_ConfirmRecieveFile(string userId, HustQQModel.FileInformation fileInfo, DateTime time)
        {
            this.Invoke(new Action(() =>
            {
                User u = FindFriendById(userId);
                contacts_BeginChat(u.name,u.qq_ID,u.note,InfoControl.FaceList[u.face]);
                FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
                if (MessageBox.Show("您是否希望接受来自" + userId + "的文件?" + "文件名:" + fileInfo.FileName + "文件大小:" + fileInfo.FileSize.ToString() + "B", "File", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {

                    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                    {
                        fileInfo.FilePath = folderBrowserDialog1.SelectedPath + "//" + fileInfo.FileName;
                    }

                    InterfaceManager.NetManager.ReplyConfirm(true);
                    return;
                }
                InterfaceManager.NetManager.ReplyConfirm(false);
            }));
        }

        SingleChat contacts_BeginChat(string name, string id, string note, Image face)
        {
            if (!InfoControl.dictSingleChat.ContainsKey(id))
            {
                SingleChat singleChatForm = new SingleChat(name, id, note, face);
                singleChatForm.Text = "与 " + name + " 聊天中";
                InfoControl.dictSingleChat.Add(id, singleChatForm);
                singleChatForm.Show();
                return singleChatForm;
            }
            else
                return InfoControl.dictSingleChat[id];
        }

        TeamChat contacts_BeginTeam(string id)
        {

            if (!InfoControl.dictTeamChat.ContainsKey(id))
            {
                TeamChat teamChatForm = new TeamChat(id);
                InfoControl.dictTeamChat.Add(id, teamChatForm);
                teamChatForm.Show();
                return teamChatForm;
            }
            else
                return InfoControl.dictTeamChat[id];
        }

        internal void Init()
        {
            //初始化主界面
            labelName.Text = InfoControl.CurrentUser.name;
            labelNote.Content = InfoControl.CurrentUser.note;
            picFace.Image = InfoControl.FaceList[InfoControl.CurrentUser.face];
            InfoControl.CurrentUser.friends=InterfaceManager.NetManager.GetFriend(InfoControl.CurrentUser.qq_ID);
            for (int i = 0; i < InfoControl.CurrentUser.friends.Count;i++ )
            {
                InfoControl.CurrentUser.friends[i] = InterfaceManager.NetManager.FindFriend(InfoControl.CurrentUser.qq_ID,InfoControl.CurrentUser.friends[i].qq_ID);
                InfoControl.CurrentUser.friends[i].qq_ID=InfoControl.CurrentUser.friends[i].qq_ID.Trim();
            }
            contacts.UpdateContacts(InfoControl.CurrentUser.friends);
            contacts.ShowContacts_Simple();
            InfoControl.CurrentUser.teams = new List<string>();
            foreach (Team t in InterfaceManager.NetManager.GetGroup(InfoControl.CurrentUser.qq_ID))
            {
                InfoControl.CurrentUser.teams.Add(t.teamID);
            }

            //foreach (User friend in InfoControl.CurrentUser.friends)
            //{
            //    InterfaceManager.NetManager.FindFriend(InfoControl.CurrentUser.qq_ID, friend.qq_ID);
            //}
        }

        private void MainFrame_FormClosed(object sender, FormClosedEventArgs e)
        {
            List<SingleChat> tmpSingle = new List<SingleChat>();
            foreach (SingleChat sc in InfoControl.dictSingleChat.Values)
                tmpSingle.Add(sc);
            InfoControl.dictSingleChat.Clear();
            while (tmpSingle.Count > 0)
            {
                tmpSingle[tmpSingle.Count - 1].Close();
                tmpSingle.RemoveAt(tmpSingle.Count - 1);
            }

            List<TeamChat> tmpTeam = new List<TeamChat>();
            foreach (TeamChat tc in InfoControl.dictTeamChat.Values)
                tmpTeam.Add(tc);
            InfoControl.dictTeamChat.Clear();
            while (tmpTeam.Count > 0)
            {
                tmpTeam[tmpTeam.Count - 1].Close();
                tmpTeam.RemoveAt(tmpTeam.Count - 1);
            }
            InterfaceManager.NetManager.Logout(InfoControl.CurrentUser.qq_ID);  //?
            InfoControl.LoginForm.Clear();
            InfoControl.LoginForm.Show();
        }

        private void MainFrame_Click(object sender, EventArgs e)
        {
            labelNote.SaveContent();
        }

        private void btnSingle_Click(object sender, EventArgs e)
        {
            #region test
            //List<User> friend = new List<User>();
            //User u1 = new User();
            //u1.online = false;
            //u1.name = "lkq";
            //u1.note = "ycx...";
            //u1.group = "Me";
            //u1.qq_ID = "12345";
            //friend.Add(u1);
            //User u2 = new User();
            //u2.online = true;
            //u2.name = "ycx";
            //u2.note = "";
            //u2.group = "aaa";
            //u2.qq_ID = "54321";
            //u2.face = 20;
            //friend.Add(u2);
            #endregion
            contacts.UpdateContacts(InfoControl.CurrentUser.friends);
            contacts.ShowContacts_Simple();
        }

        private void btnMulti_Click(object sender, EventArgs e)
        {
            contacts.ShowTeams(InfoControl.CurrentUser.teams);
        }

    }
}
